{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1]\n",
      "[1, 1]\n",
      "[1, 2, 1]\n",
      "[1, 3, 3, 1]\n",
      "[1, 4, 6, 4, 1]\n"
     ]
    }
   ],
   "source": [
    "def generate_pascals_triangle(num_rows):\n",
    "    \"\"\"\n",
    "    生成杨辉三角形的前num_rows行\n",
    "    :param num_rows: \n",
    "    :return: \n",
    "    \"\"\"\n",
    "    if num_rows <= 0:\n",
    "        return []\n",
    "    triangle = []\n",
    "    for row_num in range(num_rows):\n",
    "        row = [1] * (row_num + 1)\n",
    "        for j in range(1, row_num):\n",
    "            row[j] = triangle[row_num - 1][j - 1] + triangle[row_num - 1][j]\n",
    "        triangle.append(row)\n",
    "    return triangle\n",
    "num_rows = 5\n",
    "pascals_triangle = generate_pascals_triangle(num_rows)\n",
    "for row in pascals_triangle:\n",
    "    print(row)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.0"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
